package com.zhkj.framework.service.impl;

import com.zhkj.framework.result.ResultVO;
import com.zhkj.framework.domain.User;
import com.zhkj.framework.enums.ResultEnum;
import com.zhkj.framework.exception.CommonException;
import com.zhkj.framework.mapper.UserMapper;
import com.zhkj.framework.service.UserService;
import com.zhkj.framework.utils.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: zhangy
 * @Date: 2018/10/12 10:06
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> finaUsers() {

        List<User> users = userMapper.selectAll();
        return users;
    }

    @Override
    public User findUser(String phone) {

        return userMapper.selectbyPhone(phone);
    }

    @Transactional
    @Override
    public int deleteUser(String name) {
        return userMapper.deleteByName(name);
    }


    @Transactional
    @Override
    public ResultVO newUser(User user) {

        //判断传入用户是否为空
        if(user == null){
            throw new CommonException(ResultEnum.USER_NULL);
        }
        userMapper.insert(user);
        ResultVO resultVO = ResultVOUtil.success(user);

        return resultVO;
    }
}
